package com.thejoyrun.retrofit.model.user;

import com.google.gson.Gson;
import com.thejoyrun.retrofit.bean.User;
import com.thejoyrun.retrofit.bean.UserDetail;

import org.json.JSONObject;

import java.io.IOException;

import retrofit2.converter.gson.adapter.CustomResponseAdapter;

/**
 * Created by kkmike999 on 2016/05/08.
 *
 * 解析UserDetail数据
 */
public class UserDetailAdapter extends CustomResponseAdapter<UserDetail> {

	public UserDetailAdapter(Gson gson) {
		super(gson);
	}

	@Override
	public UserDetail fromJson(String body) throws IOException {

		// 有错误就应该抛出错误

		JSONObject json = new JSONObject(body);

		User   user     = gson.fromJson(body, User.class);
		String province = json.optString("province", "");
		String city     = json.optString("city", "");

		UserDetail detail = new UserDetail();
		detail.user = user;
		detail.province = province;
		detail.city = city;

		return detail;
	}
}
